package world.action;

import world.World;
import world.unit.Unit;

public class Move extends Action
{
	private boolean done = false;
	private double[] target;
	private Unit u;
	
	public Move(Unit u, double[] target)
	{
		this.u = u;
		this.target = target;
	}
	public void cancelAction(World w){}
	public void endAction(World w){}
	public double executeAction(double tdiff)
	{
		double dist = dist(u.getLocation(), target);
		if(dist >= u.getMovement())
		{
			dist = u.getMovement();
		}
		u.setLocation(new double[]{u.getLocation()[0]+(target[0]-u.getLocation()[0])*dist,
				u.getLocation()[1]+(target[1]-u.getLocation()[1])*dist});
		done = dist(u.getLocation(), target) == 0;
		return 0;
	}
	private double dist(double[] l1, double[] l2)
	{
		return Math.sqrt(Math.pow(l1[0]-l2[0], 2)+Math.pow(l1[1]-l2[1], 2));
	}
	public boolean isDone()
	{
		return done;
	}
	public void startAction(World w){}
}
